***********************************************************************************************************************
********** THIS .DO FILE PERFORMS A DENSITY TEST ON THE INCUMBENT SHARE (to test manipulation by incumbents) **********
***********************************************************************************************************************

clear *
use "${root}/data/processed/final_sample.dta", clear

gen incumbent_party_rank=.
forvalues i = 1(1)13 {
	replace incumbent_party_rank=`i' if (rank_`i'_sigla_partido == incumbent_party & rank_`i'_sigla_partido!="" & incumbent_party!="")	
	}

gen incumbent_party_margin=.
replace incumbent_party_margin = rank_1_vote_share-rank_2_vote_share if incumbent_party_rank==1
forvalues i = 2(1)13 {
	replace incumbent_party_margin = rank_`i'_vote_share - rank_1_vote_share if incumbent_party_rank==`i'
	}

keep incumbent_party_margin *_sample lame_duck
set scheme plotplain

* Baseline sample
keep if baseline_sample == 1

rddensity incumbent_party_margin, c(0) plot  ///
graph_opt(xtitle("Incumbent party margin") legend(off position(6)) ///
graphr(c(white))) 
graph export "${root}/results/figures/incumbent_party_manipulation_test.pdf", as(pdf) replace

* Tiebout median subsample

rddensity incumbent_party_margin if tiebout_median_sample == 1, c(0) plot   ///
graph_opt(xtitle("Incumbent party margin") legend(off position(6)) ///
graphr(c(white)) name(tiebout_median) title("Tiebout<median"))

* Coalition distance subsample

rddensity incumbent_party_margin if coal_dist_median_sample == 1, c(0) plot  ///
graph_opt(xtitle("Incumbent party margin") legend(off position(6)) ///
graphr(c(white)) name(coal_dist) title("Ideology distance>median"))

* Oil-windfalls subsample

rddensity incumbent_party_margin if oil_sample == 1, c(0) plot   ///
graph_opt(xtitle("Incumbent party margin") legend(off position(6)) ///
graphr(c(white)) name(oil) title("Oil windfall"))

graph combine tiebout_median coal_dist oil, xcommon row(2) cols(2) b1(Left margin of victory)
graph export "${root}/results/figures/incumbent_party_manipulation_tests_subsamples.pdf", replace
